# Harfbuzz package
load("@rules_cc//cc:cc_library.bzl", "cc_library")

filegroup(
    name = "basesources",
    srcs = glob(
        [
            "config*.h",
            "src/hb-*.cc",
            "src/hb-*.hh",
            "src/OT/Layout/GSUB/*.hh",
            "src/OT/Layout/GPOS/*.hh",
            "src/OT/Layout/GDEF/*.hh",
            "src/OT/Color/CBDT/*.hh",
            "src/OT/Color/CPAL/*.hh",
            "src/OT/Color/COLR/*.hh",
            "src/OT/Color/sbix/*.hh",
            "src/OT/Color/svg/*.hh",
            "src/OT/glyf/*.hh",
            "src/OT/name/*.hh",
            "src/OT/Var/VARC/*.hh",
            "src/OT/Var/VARC/*.cc",
        ],
        exclude = [
            "src/hb-coretext.cc",
            "src/hb-ft.cc",
            "src/hb-gdi.cc",
            "src/hb-cairo.cc",
            "src/hb-cairo-utils.cc",
            "src/hb-glib.cc",
            "src/hb-gobject-enums.cc",
            "src/hb-gobject-structs.cc",
            "src/hb-graphite2.cc",
            "src/hb-icu.cc",
            "src/hb-uniscribe.cc",
            "src/hb-directwrite.cc",  # exclude windows platform related files.
            "src/hb-subset.cc",
            "src/hb-subset-*.cc",
            "src/hb-subset-*.hh",
        ],
    ),
    licenses = ["notice"],
)

filegroup(
    name = "subsetsources",
    srcs = [
        "src/graph/gsubgpos-context.cc",
        "src/hb-subset.cc",
    ] + glob(
        [
            "src/hb-subset-*.cc",
            "src/hb-subset-*.hh",
            "src/graph/*.hh",
        ],
    ),
    licenses = ["notice"],
)

filegroup(
    name = "baseheaders",
    srcs = [
        "src/hb.h",
        "src/hb-aat.h",
        "src/hb-aat-layout.h",
        "src/hb-blob.h",
        "src/hb-buffer.h",
        "src/hb-common.h",
        "src/hb-config.hh",
        "src/hb-deprecated.h",
        "src/hb-draw.h",
        "src/hb-face.h",
        "src/hb-features.h",  # this header is generated by refresh-harfbuzz.sh
        "src/hb-font.h",
        "src/hb-map.h",
        "src/hb-ot.h",
        "src/hb-ot-color.h",
        "src/hb-ot-deprecated.h",
        "src/hb-ot-font.h",
        "src/hb-ot-layout.h",
        "src/hb-ot-math.h",
        "src/hb-ot-meta.h",
        "src/hb-ot-metrics.h",
        "src/hb-ot-name.h",
        "src/hb-ot-shape.h",
        "src/hb-ot-var.h",
        "src/hb-set.h",
        "src/hb-shape.h",
        "src/hb-shape-plan.h",
        "src/hb-unicode.h",
        "src/hb-version.h",
    ],
    licenses = ["notice"],
)

harfbuzz_copts = [
    "-w " +
    "-DHAVE_CONFIG_H " +
    "-DHAVE_OT " +
    "-Ithird_party/harfbuzz " +
    "-Ithird_party/icu/include",
]

cc_library(
    name = "harfbuzz",
    srcs = [
        "src/hb-ft.cc",
        "src/hb-icu.cc",
    ] + select({
        "@platforms//os:osx": [],
        "//conditions:default": [
            "src/hb-glib.cc",
        ],
    }),
    hdrs = [
        "src/hb-ft.h",
        "src/hb-icu.h",
        ":baseheaders",
    ] + select({
        "@platforms//os:osx": [],
        "//conditions:default": [
            "src/hb-glib.h",
        ],
    }),
    copts = harfbuzz_copts + [
        "-DHB_EXPERIMENTAL_API",
        "-DHB_NO_BEYOND_64K",
        "-DHB_NO_VISIBILITY",
        "-std=c++17",
    ] + select({
        "@platforms//os:osx": ["-DHAVE_XLOCALE_H"],
        "//conditions:default": [],
    }),
    includes = ["src"],
    visibility = ["//visibility:public"],
    deps = [
        ":harfbuzz-subset",
        ":loose_headers",
        "@freetype",
        "@icu//icu4c/source/common:platform",
    ] + select({
        "@platforms//os:osx": [],
        "//conditions:default": [
            "@glib//glib",
        ],
    }),
)

config_setting(
    name = "redistributable",
    values = {"define": "HARFBUZZ_GLIB_REDIST=1"},
)

cc_library(
    name = "harfbuzz-subset",
    srcs = [
        ":basesources",
        ":subsetsources",
    ],
    hdrs = [
        "config.h",
        "config-harfbuzz-default-ucdn.h",
        "src/hb-subset.h",
        "src/hb-subset-serialize.h",
        "src/hb-ucd-table.hh",
        ":baseheaders",
    ],
    copts = harfbuzz_copts + [
        "-DHB_NO_VISIBILITY",
        "-DUCDN_NO_GOOGLE",
        "-DHB_EXPERIMENTAL_API",
        "-DHB_NO_BEYOND_64K",
        "-DSUBSET",
        "-std=c++17",
    ] + select({
        "@platforms//os:osx": ["-DHAVE_XLOCALE_H"],
        "//conditions:default": [],
    }),
    includes = [
        "src",
    ],
    deps = [
        ":loose_headers",
        "@freetype",
        "@glib//glib",
        "@icu//icu4c/source/common:platform",
    ],
)

cc_library(
    name = "loose_headers",
    tags = ["avoid_dep"],
    textual_hdrs = [
        "src/OT/Layout/Common/Coverage.hh",
        "src/OT/Layout/Common/CoverageFormat1.hh",
        "src/OT/Layout/Common/CoverageFormat2.hh",
        "src/OT/Layout/Common/RangeRecord.hh",
        "src/OT/Layout/types.hh",
        "src/hb-static.cc",
        "test/api/hb-test.h",
        "test/api/test-ot-face.c",
        "test/fuzzing/hb-fuzzer.hh",
    ] + glob([
        "src/*.hh",
        "src/*.h",
    ]),
    visibility = ["//visibility:private"],
)
